package com.hx.controller;

import com.hx.bean.User;
import com.hx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     *
     * @param username 用户名
     * @param password 密码
     * @param valcode 验证码
     * @return
     */
    @ResponseBody
    @RequestMapping(produces = "application/json;charset=UTF-8",value = "login")
    public String login(@RequestParam(name = "username")final String username,
                        @RequestParam(name ="password",required = false)final String password,
                        @RequestParam(name ="valcode",required = false)final String valcode){

        Map<String, Object> map = userService.loginUserLock(username);
        if((boolean)map.get("flag")){
            return  "登录失败，因" + username +"用户超过了限制登录次数，已被禁止登录，还剩：" + map.get("lockTime") +"分钟";
        }else{
           User user =  userService.login(username,password);
           if(user!= null){

               return "/succ,jsp";

           }else{

               String result = userService.loginValdate(username);
               return result;
           }

        }


    }

}
